#include "interrupt.h"

#define PIC_IRQ_STATUS (*(volatile unsigned *)0x14000000)
#define PIC_IRQ_ENABLESET (*(volatile unsigned *)0x14000008)
#define PIC_IRQ_ENABLECLR (*(volatile unsigned *)0x1400000c)

static void (* irq_handlers[32])(void);

void irq_entry(void)
{
    unsigned status = PIC_IRQ_STATUS;
    for (int i = 0; i < 32; i++)
    {
        if ((status & (1 << i)) && irq_handlers[i])
        {
            irq_handlers[i]();
        }
    }
}

void set_irq_handler(int interrupt, void (*handler)(void))
{
    irq_handlers[interrupt] = handler;
}

void disable_irq_interrupt(int interrupt)
{
    PIC_IRQ_ENABLECLR = 1 << interrupt;
}

void enable_irq_interrupt(int interrupt)
{
    PIC_IRQ_ENABLESET |= 1 << interrupt;
}
